﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using Common;

namespace MODEL
{
    public class StationInfo:ViewModelBase,ICheck
    {
        public int StationId { get; set; }
        public string StationName { get; set; }
        public string StationCode { get; set; }
        public string StationType { get; set; }        
        public string StationAddress { get; set; }
        public string CountyName { get; set; }
        public string CityName { get; set; }
        public string ProvinceName { get; set; }
        public float? Lon { get; set; }
        public float? Lat { get; set; }
        public string DeviceName { get; set; }
        public string HardWareId { get; set; }
        public long HardWareCode { get; set; }       
        public int CtrlFunction { get; set; }
        public string Version { get; set; }
        public string Battery { get; set; }
        public string Ip { get; set; }
        public string Sim { get; set; }
        public int? HeartBeatInterval { get; set; }
        public string Note { get; set; }
        public int Enabled{ get; set; }
        public string ElementList { get; set; }

        private string m_ValveNoListStr;
        public string Valve_NoListStr
        {
            get { return this.m_ValveNoListStr; }
            set {
                this.m_ValveNoListStr = value;
                if (string.IsNullOrEmpty(this.m_ValveNoListStr))
                {
                    return;
                }
                try
                {
                    m_ValveNoListStr.Split('>').ToList().ForEach(v =>
                    {
                        string[] valveWithOperate = v.Split(':');
                        if (valveWithOperate.Length < 2)
                        {
                            return;
                        }
                        m_StationOperateDic.Add(int.Parse(valveWithOperate[0]), (StationOperateTypeEnum)int.Parse(valveWithOperate[1]));
                    });
                }
                catch
                {
                    //not process
                }

            }
        }

        public List<int> ValveNoList
        {
            get
            {
                if (this.m_StationOperateDic.SafeCheck())
                {
                    return m_StationOperateDic.Select(s => s.Key).ToList();
                }
                return new List<int>();
            } 
        }

        public StationOperateTypeEnum GetOperateTypeEnum(int valveNo)
        {
            if (this.m_StationOperateDic.SafeCheck())
            {
                try
                {
                    return this.m_StationOperateDic[valveNo];
                }
                catch
                {
                    return StationOperateTypeEnum.NoOperate;
                }
            }
            return StationOperateTypeEnum.NoOperate;
        }

        private Dictionary<int, StationOperateTypeEnum> m_StationOperateDic = new Dictionary<int, StationOperateTypeEnum>();

        private bool m_IsChecked = false;
        public bool IsChecked
        {
            get { return this.m_IsChecked; }
            set { this.m_IsChecked = value; base.RaisePropertyChanged(() => this.IsChecked); }
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }
            return ((StationInfo)obj).StationId == this.StationId;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public bool IsContainsElement(string elementCode)
        {
            if (string.IsNullOrWhiteSpace(this.ElementList))
            {
                return false;
            }

            return ElementList.Split(',').Contains(elementCode, StringComparer.OrdinalIgnoreCase);
        }
    }
}
